//
//  RmdStoryDetailHeaderView.m
//  Travel assistant
//
//  Created by 杨帆 on 16/3/14.
//  Copyright © 2016年 旅游助手. All rights reserved.
//

#import "RmdStoryDetailHeaderView.h"
static const CGFloat kVerMargin = 5;
static const CGFloat kHorMargin = 10;

static const CGFloat kTopHeight = 160;

#define kBgColor [UIColor whiteColor]

@interface RmdStoryDetailHeaderView ()

@property(nonatomic,strong)UILabel *userNameL;
@property(nonatomic,strong)UILabel *titleNameL;
@property(nonatomic,strong)UIView *backView;
@property(nonatomic,strong)UIImageView *userImageV;
@property(nonatomic,strong)UILabel *poiNameL;
@property(nonatomic,strong)UILabel *date_tourL;
@property(nonatomic,strong)UILabel *textL;

@end

@implementation RmdStoryDetailHeaderView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.userNameL = [[UILabel alloc]initWithFrame:(CGRectMake(10, 40, KScreenWidth-20, 0.03*KScreenHeight))];
        self.userNameL.numberOfLines = 0;
        self.userNameL.textAlignment = NSTextAlignmentCenter;
        self.userNameL.font= [UIFont fontWithName:@"Helvetica-Bold" size:14];
        self.userNameL.textColor = [UIColor whiteColor];
        [self addSubview:self.userNameL];
        
        self.titleNameL = [[UILabel alloc]initWithFrame:(CGRectMake(20, 0.03*KScreenHeight+40, KScreenWidth-40, 0.1*KScreenHeight))];
        
        self.titleNameL.font= [UIFont fontWithName:@"Helvetica-Bold" size:24];
        self.titleNameL.textAlignment = NSTextAlignmentCenter;
        self.titleNameL.textColor = [UIColor whiteColor];
        [self addSubview:self.titleNameL];
        
        self.backView = [[UIView alloc]initWithFrame:(CGRectMake(0, kTopHeight, KScreenWidth, 100))];
        self.backView.layer.cornerRadius = 5;
        self.backView.layer.masksToBounds = YES;
        self.backView.backgroundColor = kBgColor;
        [self addSubview:self.backView];
        
        self.userImageV = [[UIImageView alloc]initWithFrame:(CGRectMake(10, kTopHeight + 20,50 , 50))];
        self.userImageV.layer.cornerRadius = 25;
        self.userImageV.layer.masksToBounds = YES;
        [self addSubview:self.userImageV];
        
        self.poiNameL = [[UILabel alloc]initWithFrame:(CGRectMake(65, kTopHeight + 20, 0.76*KScreenWidth, 0.03*KScreenHeight))];
        
        self.poiNameL.font= [UIFont fontWithName:@"Helvetica-Bold" size:18];
        self.poiNameL.textAlignment = NSTextAlignmentLeft;
        [self addSubview:self.poiNameL];
        
        self.date_tourL = [[UILabel alloc]initWithFrame:(CGRectMake(65, self.poiNameL.bottom + 10, 0.56*KScreenWidth, 0.02*KScreenHeight))];
        
        self.date_tourL.font= [UIFont fontWithName:@"Helvetica-Bold" size:12];
        self.date_tourL.textAlignment = NSTextAlignmentLeft;
        [self addSubview:self.date_tourL];
        
        self.textL = [[UILabel alloc]initWithFrame:(CGRectMake(10, 0.41*KScreenHeight, KScreenWidth-20, 0.4*KScreenHeight))];
        self.textL.numberOfLines = 0;
        self.textL.font = [UIFont fontWithName:@"Helvetica-Bold" size:12];
        self.textL.textAlignment = NSTextAlignmentLeft;
        [self addSubview:self.textL];
        
        
    }
    return self;
}



- (void)setData:(RmdStoryDetailBannerItem *)item {
    
    self.item = item;
    self.userNameL.text = [NSString stringWithFormat:@"此故事由 %@ 收录",item.user.name];
    self.titleNameL.text = item.name;
    [self.userImageV sd_setImageWithURL:[NSURL URLWithString:item.user.avatar_l] placeholderImage:nil];
    if (item.poi) {
        self.poiNameL.text = [NSString stringWithFormat:@"在 %@ 的故事",item.poi.name];
    }else{
        self.poiNameL.text =item.name;
    }
    NSArray *array = [item.date_tour componentsSeparatedByString:@"T"];
    NSString *yearString =array[0];
    NSArray *arrayTime = [array[1] componentsSeparatedByString:@"+"];
    NSString *timeString = arrayTime[0];
    
    self.date_tourL.text =[NSString stringWithFormat:@"%@ %@",yearString,timeString];
    
    CGFloat cellHeight = self.userImageV.bottom;
    cellHeight += kVerMargin;
    
    if (isEmptyString(item.text)) {
        self.textL.hidden = YES;
    } else {
        self.textL.hidden = NO;
        self.textL.text = item.text;
        CGSize size = [item.text sizeWithFontCompatible:self.textL.font constrainedToSize:CGSizeMake(self.textL.width, CGFLOAT_MAX)];
        self.textL.frame = CGRectMake(kHorMargin, cellHeight, self.textL.width, size.height);
        
        cellHeight += size.height;
        cellHeight += kVerMargin;
    }
    
    [self.backView setHeight:cellHeight - kTopHeight];
    
    [self setHeight:cellHeight];
}




@end
